<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/16
 * Time: 下午18:07
 */

class ApiStoreController extends BaseController {


    /**
     * @Author:      sqq
     * @DateTime:    2018-08-16 18:07:29
     * @Description: 门店-城市-区县
     * @Param: $city_id Int 城市id
     * @link: http://www.activity.com/wechat/api3.0/store/store_city_region?city_id=1
     */
    public function store_city_region(){
        $validator = Validator::make(Input::all(),
            array(
                'city_id' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            //获取参数
            $city_id = Input::get('city_id');

            //根据城市id获取当前城市已开放的区县
            $region = MaintenanceShops::getRegion($city_id);

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $region
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "store_city_region", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-08-16 18:07:29
     * @Description: 门店列表
     * @Param: $city_id Int 城市id
     * @Param: $region_id Int 区县id
     * @link: http://www.activity.com/wechat/api3.0/store/storeList?city_id=1&region_id=1
     */
    public function storeList(){
        $validator = Validator::make(Input::all(),
            array(
                'city_id' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
           // $city = ResourceCityType::getCity(Input::get('city'));

            //获取参数
            $city_id = Input::get('city_id');
            $region_id = Input::get('region_id');

            //根据城市和区县获取门店信息
            //$store = MaintenanceShops::getShopWechat3($city_id,$region_id);
            $store = AhpMaintenanceShops::getShopWechat3($city_id,$region_id);  //2018-11-17更改为：数据取用ahp后台数据 sqq
            if(is_null($store)){
                $store = '';
            }else{
                foreach($store as $key => $s){
                    //$s->store_img_url = Config::get('config.store_img_url');
                    //$s->store_img_url = Config::get('config.shop_img_url');
                    $s->store_img_url = Config::get('store_wechat3.store_wechat3_file_url');
                }
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $store
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "store_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:   2018-08-17 10:26:29
     * @Description: 门店详情
     * @Param: $shop_id Int 门店id
     * @link：http://www.activity.com/wechat/api3.0/store/store_details?shop_id=1
     */
    public function storeDetails(){
        $validator = Validator::make(Input::all(),
            array(
                'shop_id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $maintenance_shops = AhpMaintenanceShops::getDetails(Input::get('shop_id'));
            //$maintenance_shops = MaintenanceShops::getDetails(Input::get('shop_id'));
            if(!is_null($maintenance_shops)) {
                $Evaluation = Orders::leftjoin('order_evaluation as oe','orders.order_id','=','oe.order_id')->where('orders.shop_id',$maintenance_shops->id)
                    ->where('oe.is_show',1)->where('orders.status',1018)->avg('oe.star');
                $star = ceil($Evaluation);
                if((int)$star === 0){
                    $maintenance_shops->star = 5;
                }else if((int)$star < 3){
                    $maintenance_shops->star = 3;
                }else{
                    $maintenance_shops->star = $star;
                }
                $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
                $order_count = Orders::getToMaintenanceShops(isset($wechat_user->id)?$wechat_user->id:Null,Input::get('shop_id'));
                if($order_count != 0){
                    $maintenance_shops->to_shop = 1;
                }else{
                    $maintenance_shops->to_shop = 0;
                }
                //$maintenance_shops->store_img_url=Config::get('config.store_img_url');
                //$maintenance_shops->store_img_url=Config::get('config.shop_img_url');
                $maintenance_shops->store_img_url = Config::get('store_wechat3.store_wechat3_file_url');
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $maintenance_shops,
            );
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "storeDetails", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-09-19 10:36
     * @Description:     api3.0 门店服务
     * @param string $token             用户token
     * @param int $model_id             车辆车型id
     * @link ：http://www.activity.com/wechat/api3.0/store/storeService?shop_id=1&city=%E5%8C%97%E4%BA%AC%E5%B8%82
     */
    public function storeService(){
        $validator = Validator::make(Input::all(),
            array(
                'shop_id' => 'required',
                'city' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            if(Input::get("token")){
                $users = $this->verificationUserToken(Input::get("token"),$response);
                if(is_null($users)){
                    break;
                }
            }else{
                $users = (object)[];
                $users->id = 0;
            }

            //获取参数
            $shop_id = Input::get('shop_id');
            $city_name = Input::get('city');
            header("Content-type:text/html;charset=utf-8");
            //判断城市是否开放
            $city_id = PublicApi::Judge_city($city_name);
            if($city_id == 0){
                //城市未开放
                $response = Lang::get("error.service_list_city_empty_error_30011");
                break;
            }

            //判断当前城市是否有门店开放
            $shop = MaintenanceShops::where('status',1)->where('city_id',$city_id)->where('is_show',1)->first();
            if(empty($shop)){
                //城市没有适配门店
                $response = Lang::get("error.service_list_city_shop_empty_error_30012");
                break;
            }

            $shops = AhpMaintenanceShops::where('cms_shop_id',$shop_id)->where('status',1)->first();
            if(is_null($shops)){
                $response = Lang::get("error.wechat3_shops_not_exist_error_30000");
                break;
            }
            $array_id = array();
            if($shops->is_service == 2){
                $array_service = AhpMaintenanceShopsService::where('cms_shop_id',$shop_id)->where('status',1)->orderBy('sort','asc')->get()->toArray();
                $array_id = array_column($array_service,'service_id');
            }
            $service = AhpService::where('status',1)->select('id','service_name','classify','service_type','thumbnail','first_picture');
            if(!empty($array_id)){
                $service = $service->whereIn('id',$array_id);
            }
            $service = $service->get();
            $ahp_service = [];
            if(!is_null($service)){
                $ahp_service = $service->toArray();
                //$ahp = $ahp_service;
                foreach($ahp_service as $key => $val) {
                    $ahp_service[$key]['url'] = Config::get('config.ahp_upload_files_url');
                    //查询ahp服务所包含的cms/mkt服务
                    if ($val['service_type'] == 1) {
                        //mkt活动包
                        $ahp_service[$key]['service'] = AhpServiceMktCms::join('product_activity', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_activity.id')->where('ahp_service_mkt_cms.service_id', $val['id'])->select('product_activity.id', 'product_activity.name', 'price', 'product_activity.type', 'discount')->get();
                    } else {
                        //cms基础包
                        $ahp_service[$key]['service'] = AhpServiceMktCms::join('product_package', 'ahp_service_mkt_cms.mkt_cms_id', '=', 'product_package.id')->where('ahp_service_mkt_cms.service_id', $val['id'])->select('product_package.id', 'product_package.name')->get();
                    }
                    if($ahp_service[$key]['service']){
                        $ahp_service[$key]['service'] = $ahp_service[$key]['service']->toArray();
                        foreach($ahp_service[$key]['service'] as $s_key => $s_val){
                            //判断是否适配城市、门店
                            if($shop_id && $val['service_type'] == 1){
                                //判断适配门店
                                $product = ProductActivityShop::where('product_activity_id',$s_val['id'])->where('shop_id',$shop_id)->first();
                            }else{
                                //判断适配城市
                                if($val['service_type'] == 1){
                                    $table = 'ProductActivityCity';
                                    $id = 'product_activity_id';
                                }else{
                                    $table = 'ProductPackageCity';
                                    $id = 'pack_id';
                                }
                                $product = $table::where($id,$s_val['id'])->where('city_id',$city_id)->where('status',1)->first();
                            }
                            if(is_null($product)){
                                if (isset($ahp_service[$key]['service'][$s_key])) {
                                    unset($ahp_service[$key]['service'][$s_key]);
                                    continue;
                                }
                            }
                        }
                    }else{
                        unset($ahp_service[$key]);
                    }
                }
            }
            if ($ahp_service) {
                foreach ($ahp_service as $key => $val) {
                    if (!$val['service']) {
                        unset($ahp_service[$key]);
                    }
                }
            }
            if($shops->is_service != 2) {
                $ahp_service = array_values($ahp_service);
                $ahp_service = array_slice($ahp_service, 0, 8, false);
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $ahp_service);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "wechat/api3.0/storeService", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}